package com.example.demo.thread;

public class Demo5 {

    private static final Object lock = new Object();

    public static class MyThread implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                for (int i = 0; i < 100; i++) {
                    System.out.println("Thread A " + i);
                }
            }
        }
    }

    public static class MyThread2 implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                for (int i = 0; i < 100; i++) {
                    System.out.println("Thread B " + i);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Thread a = new Thread(new MyThread());
        Thread b = new Thread(new MyThread2());

        a.start();
        //a.join();
        b.start();
    }
}
